﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;
using System.IO;

namespace Booking_Hotel
{
    public partial class Supplier_Images : System.Web.UI.Page
    {
        ThanhBHotelDataContext tbk = new ThanhBHotelDataContext();
        Supplier_Hotel_Image entities = new Supplier_Hotel_Image();
        int ImageID;
        string File_NameImages;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["ss_SupplierID"] = 2;//Gan SupplierID cho Dropdownlist de lay DS khach san cua Supplier
                ShowImage();
            }
        }

        protected void ShowImage()
        {
            var image = from dv in tbk.Supplier_Hotels
                        join shi in tbk.Supplier_Hotel_Images on dv.Supplier_HotelID equals shi.Supplier_HotelID
                        where dv.SupplierID == (int)Session["ss_SupplierID"]
                        select new
                        {
                            Supplier_Hotel_ImageID = shi.Supplier_hotel_ImageID,
                            Supplier_HotelID = shi.Supplier_HotelID,
                            TenKhachSan = dv.TenKhachSan,
                            ImagePath = shi.ImagePath,
                            Description = shi.Description,
                            StatusAnhDaiDien= shi.StatusAnhDaiDien
                        };
            GridView1.DataSource = image;
            GridView1.DataBind();

        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow select = GridView1.Rows[e.NewSelectedIndex];
            Session["ss_Supplier_Hotel_ImagesID"] = int.Parse(select.Cells[0].Text.Trim());
            DDL_TenKhachSan.SelectedValue = select.Cells[1].Text.Trim();
            //lbl_imagepath.Text = select.Cells[2].va;
            txt_Desciption.Text = select.Cells[4].Text.Trim();
            CB_TrangThaiAnh.Checked= ((CheckBox)select.Cells[5].Controls[0]).Checked;
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                e.Row.Cells[0].Visible = false;
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton bt_Delete = sender as ImageButton;
            GridViewRow row = (GridViewRow)bt_Delete.NamingContainer;
            ImageID = int.Parse(row.Cells[0].Text.Trim());//Supplier_Hotel_DichVuID trên lưới.
            Supplier_Hotel_Image shi = tbk.Supplier_Hotel_Images.Where(c => c.Supplier_hotel_ImageID == ImageID).Single();
            string pathfile = shi.ImagePath;
            if (pathfile != "")
            {
                string strImagePath_Delete = Server.MapPath(pathfile);
                File.Delete(strImagePath_Delete);
            }
            tbk.Supplier_Hotel_Images.DeleteOnSubmit(shi);
            tbk.SubmitChanges();
             lbl_imagepath.Text = "";
            ShowImage();
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            lbl_imagepath.Text = "";
            try
            {
                File_NameImages = FU_Hotel.PostedFile.FileName;
                entities = new Supplier_Hotel_Image();
                entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                entities.Description = txt_Desciption.Text.Trim();
              //  entities.StatusAnhDaiDien = CB_TrangThaiAnh.Checked;
                if (CB_TrangThaiAnh.Checked == true)
                {
                    entities.StatusAnhDaiDien = true;
                    var check = from p in tbk.Supplier_Hotel_Images
                                where p.StatusAnhDaiDien == true
                                select p;
                    foreach (Supplier_Hotel_Image sh in check)
                    {
                        sh.StatusAnhDaiDien = false;
                    }
                    tbk.SubmitChanges();
                }
                else
                {
                    entities.StatusAnhDaiDien = false;
                }

                if ((this.FU_Hotel.PostedFile.ContentType == "image/jpeg") || (File_NameImages == ""))
                {
                    if (FU_Hotel.PostedFile.ContentLength < 204800)
                    {
                        string KTTen = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
                        string strImagePath_folder = Server.MapPath("~/images_information/Hotel_Image/" + DDL_TenKhachSan.Text.Trim());
                        Directory.CreateDirectory(strImagePath_folder);
                        HttpPostedFile file = this.FU_Hotel.PostedFile;

                        string strImagePath = "~/images_information/Hotel_Image/" + this.DDL_TenKhachSan.Text.Trim() +"/"+ KTTen + ".jpg";
                        string strImagePath_savefile = Server.MapPath("~/images_information/Hotel_Image/" + this.DDL_TenKhachSan.Text.Trim() + "/" + KTTen + ".jpg");

                        if (File_NameImages != "")
                        {
                            FU_Hotel.SaveAs(strImagePath_savefile);
                            entities.ImagePath = strImagePath.Trim();
                        }
                        else
                        {
                            entities.ImagePath = "";
                        }
                       
                        Supplier_ImageDAO si = new Supplier_ImageDAO();
                        si.addSupplier_Hotel_Image(entities);
                        ShowImage();
                    }
                    else
                    {
                        Booking_Hotel.DAL.MyInclude.MessageBox.ShowMessage("Images upload khong duoc qua 200kb", this.Page);
                    }
                }
                else
                {
                    Booking_Hotel.DAL.MyInclude.MessageBox.ShowMessage("Images upload phai o dinh dang JPG(JPEG)", this.Page);
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void n_Update_Click(object sender, EventArgs e)
        {
            try
            {
                File_NameImages = FU_Hotel.PostedFile.FileName;
                int check_Supplier_Hotel_ImageID = (int)(Session["ss_Supplier_Hotel_ImagesID"]);
                if (check_Supplier_Hotel_ImageID != null)
                {
                    entities = new Supplier_Hotel_Image();
                    entities.Supplier_hotel_ImageID = check_Supplier_Hotel_ImageID;
                    entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                    entities.Description = txt_Desciption.Text.Trim();
                    if (CB_TrangThaiAnh.Checked == true)
                    {
                        entities.StatusAnhDaiDien = true;
                        var check = from p in tbk.Supplier_Hotel_Images
                                    where p.StatusAnhDaiDien == true && p.Supplier_hotel_ImageID != check_Supplier_Hotel_ImageID
                                    select p;
                        foreach (Supplier_Hotel_Image sh in check)
                        {
                            sh.StatusAnhDaiDien = false;
                        }
                        tbk.SubmitChanges();
                    }
                    else
                    {
                        entities.StatusAnhDaiDien = false;
                    }
                    if ((this.FU_Hotel.PostedFile.ContentType == "image/jpeg") || (File_NameImages == ""))
                    {

                        Supplier_Hotel_Image shi = tbk.Supplier_Hotel_Images.Where(c => c.Supplier_hotel_ImageID == check_Supplier_Hotel_ImageID).Single();
                        string pathfile = shi.ImagePath;
                        string KTTen = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
                        if (FU_Hotel.PostedFile.ContentLength < 204800)
                        {
                            if (File_NameImages != "")
                            {   
                                string strImagePath_folder = Server.MapPath("~/images_information/Hotel_Image/" + DDL_TenKhachSan.Text.Trim());
                                HttpPostedFile file = this.FU_Hotel.PostedFile;
                                string strImagePath = "~/images_information/Hotel_Image/" + this.DDL_TenKhachSan.Text.Trim() + "/" +KTTen+ ".jpg";
                                string strImagePath_savefile = Server.MapPath("~/images_information/Hotel_Image/" + DDL_TenKhachSan.Text.Trim() + "/" + KTTen + ".jpg");
                                FU_Hotel.SaveAs(strImagePath_savefile);
                                entities.ImagePath = strImagePath.Trim();
                                string strImagePath_Delete = Server.MapPath(pathfile);
                                File.Delete(strImagePath_Delete);
                            }
                            else
                            {
                                entities.ImagePath = pathfile;
                            }
                            Supplier_ImageDAO si = new Supplier_ImageDAO();
                            si.updateSupplier_Hotel_Image(entities);
                            ShowImage();
                        }
                        else
                        {
                            Booking_Hotel.DAL.MyInclude.MessageBox.ShowMessage("Images upload khong duoc qua 200kb", this.Page);
                        }
                    }
                    else
                    {
                        Booking_Hotel.DAL.MyInclude.MessageBox.ShowMessage("Images upload phai o dinh dang JPG(JPEG)", this.Page);
                    }
                   
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Vui lòng chọn đối tượng cần update dưới lưới", this.Page);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}